/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.createUniqueName;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.UMLFactory;

/**
 * Command to create instances of properties. Currently, properties are
 * only created if the owner-to-be is a Class or an Interface.
 * 
 * <p>The following attributes must be set for the command to complete
 * successfully:</p>
 * <ul>
 *   <li>Owner
 * </ul>
 * 
 * <p>The following attributes can be read to access the result of the 
 * command:</p>
 * <ul>
 *   <li>Property
 * </ul>
 *
 * @author frankd@ifi.uio.no
 */
public class CreatePropertyCommand extends Command {
	
	private Namespace owner;
	private Property property;
	
	public void setOwner(final Namespace owner) {
		this.owner = owner;
	}
	
	public Property getProperty() {
		return property;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void execute() {
		if (owner instanceof org.eclipse.uml2.uml.Class) {
			org.eclipse.uml2.uml.Class context = (org.eclipse.uml2.uml.Class) owner;
			property = UMLFactory.eINSTANCE.createProperty();
			property.setName(createUniqueName(context, "Property"));
			context.getOwnedAttributes().add(property);
		}
		else if (owner instanceof org.eclipse.uml2.uml.Interface) {
			org.eclipse.uml2.uml.Interface context = (org.eclipse.uml2.uml.Interface) owner;
			property = UMLFactory.eINSTANCE.createProperty();
			property.setName(createUniqueName(context, "Property"));
			context.getOwnedAttributes().add(property);
		}
	}

	@Override
	public void undo() {
		if (property != null) {
			property.getClass_().getOwnedAttributes().remove(property);
			property.setName(null);
			property = null;
		}
	}
	
	@Override
	public void dispose() {
		owner = null;
		property = null;
	}	
}
